package org.chen.ctrip.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.chen.common.ctrip.dto.ProductMultilangRequest;
import org.chen.common.ctrip.entity.Product;
import org.chen.common.ctrip.entity.ProductMultilang;
import org.chen.common.result.ResultVO;
import org.chen.ctrip.service.ProductService;
import org.chen.ctrip.service.impl.ProductMultilangServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/multilang")
@Slf4j
@Tag(name = "产品多语言接口", description = "处理产品多语言内容的相关接口")
public class ProductMultilangController {

    @Autowired
    private ProductService productService;

    @Autowired
    private ProductMultilangServiceImpl productMultilangService;

    @GetMapping("/{productId}")
    @Operation(summary = "获取产品多语言内容", description = "根据产品ID和语言代码获取对应的多语言内容")
    public ResultVO<ProductMultilangResponse> getProductMultilang(
            @Parameter(description = "产品ID") @PathVariable String productId,
            @Parameter(description = "语言代码，如：zh, en, ja等") @RequestParam(defaultValue = "zh") String lang) {

        try {
            log.info("获取产品多语言内容，产品ID: {}, 语言: {}", productId, lang);

            // 获取基础产品信息
            Product product = productService.getById(productId);
            if (product == null) {
                return ResultVO.fail("产品不存在: " + productId);
            }

            // 获取多语言内容
            ProductMultilang multilangContent = productMultilangService.getProductMultilangContent(productId, lang);

            // 构建响应
            ProductMultilangResponse response = new ProductMultilangResponse();
            response.setMultilangContent(multilangContent);
            response.setLanguageCode(lang);

            return ResultVO.success(response);

        } catch (Exception e) {
            log.error("获取产品多语言内容失败，产品ID: {}, 语言: {}", productId, lang, e);
            return ResultVO.fail("获取多语言内容失败: " + e.getMessage());
        }
    }

    @PostMapping("/{productId}/refresh")
    @Operation(summary = "刷新产品多语言内容", description = "强制重新生成指定产品的多语言内容")
    public ResultVO<String> refreshProductMultilang(
            @Parameter(description = "产品ID") @PathVariable String productId,
            @Parameter(description = "语言代码") @RequestParam String lang) {

        try {
            log.info("刷新产品多语言内容，产品ID: {}, 语言: {}", productId, lang);

            productMultilangService.refreshMultilangContent(productId, lang);

            return ResultVO.success("刷新成功");

        } catch (Exception e) {
            log.error("刷新产品多语言内容失败，产品ID: {}, 语言: {}", productId, lang, e);
            return ResultVO.fail("刷新失败: " + e.getMessage());
        }
    }


    @PostMapping("/saveProductMultilang")
    @Operation(summary = "保存产品多语言内容", description = "保存或更新指定产品的多语言内容")
    public ResultVO<String> saveProductMultilang(@RequestBody ProductMultilangRequest multilang) {
        try {
            log.info("保存产品多语言内容，产品ID: {}, 语言: {}", multilang.getProductId(), multilang.getLanguageCode());

            // 简单验证
            if (multilang.getProductId() ==null || multilang.getProductId().isEmpty() || multilang.getLanguageCode().isEmpty()) {
                return ResultVO.fail("产品ID和语言代码不能为空");
            }

            if (multilang.getPageContentMarkdown() == null || multilang.getPageContentMarkdown().isEmpty() || multilang.getTranslatedContent().isEmpty()) {
                return ResultVO.fail("产品内容和翻译不能为空");
            }

            // 保存或更新
            productMultilangService.saveMultilang(multilang);

            return ResultVO.success("保存成功");
        } catch (Exception e) {
            log.error("保存产品多语言内容失败，产品ID: {}, 语言: {}", multilang.getProductId(), multilang.getLanguageCode(), e);
            return ResultVO.fail("保存失败: " + e.getMessage());
        }
    }
    /**
     * 响应数据结构
     */
    @Data
    public static class ProductMultilangResponse {
        private ProductMultilang multilangContent;
        private String languageCode;
    }
}